package com.qianfeng.zhangwei.sybl.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.gc.flashview.FlashView;
import com.gc.flashview.listener.FlashViewListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.qianfeng.zhangwei.sybl.R;
import com.qianfeng.zhangwei.sybl.adapter.ActivityAdapter;
import com.qianfeng.zhangwei.sybl.adapter.AppBaseAdapter;
import com.qianfeng.zhangwei.sybl.adapter.ImageAndTextAdapter;
import com.qianfeng.zhangwei.sybl.bean.Activities;
import com.qianfeng.zhangwei.sybl.bean.Banner;
import com.qianfeng.zhangwei.sybl.bean.Entity;
import com.qianfeng.zhangwei.sybl.bean.H5HotCate;
import com.qianfeng.zhangwei.sybl.bean.ViewHolder;
import com.qianfeng.zhangwei.sybl.config.UrlContants;
import com.qianfeng.zhangwei.sybl.utils.AsyncHttpUtils;

import java.util.ArrayList;

/**
 * @说 明：
 * @项目名称：Sz1504
 * @包 名： com.qianfeng.zhangwei.sybl.fragment
 * @类 名： TestFragment
 * @创 建人： zhangwei
 * @创建时间：2015-07-30 16:59
 * @版 本：v1.0
 * @修 改人：
 * @修改时间：
 * @修改备注：
 */
public class TestFragment extends PullToRefereshFragment<Activities> {
    private HeadHolder holder;

    @Override
    public void addHeadView(ListView listView) {
        holder = new HeadHolder(inflater, R.layout.widget_game_head_layout);
        listView.addHeaderView(holder.view);
    }

    @Override
    public void setAdapter(ListView listView) {
        adapter = new ActivityAdapter(list, getActivity());
        listView.setAdapter(adapter);
    }

    @Override
    public void loadData(PullToRefreshListView ptrListView, AppBaseAdapter adapter, int mCurrentPage) {
        TypeReference<Entity<Activities>> typeReference = new TypeReference<Entity<Activities>>() {
        };
        AsyncHttpUtils.getInstance().post(UrlContants.URL_ACTIVITY, typeReference, getRequsetParams(), ptrListView, list, adapter, mCurrentPage);
    }

    private RequestParams getRequsetParams() {
        RequestParams params = new RequestParams();
        return params;
    }

    /**
     * ListView头部信息
     */

    private class HeadHolder extends ViewHolder implements AdapterView.OnItemClickListener, FlashViewListener {
        private FlashView flashView;
        private ArrayList<Banner> banners = new ArrayList<>();
        /**
         * 热门推荐
         */
        private GridView gridView;
        private ImageAndTextAdapter adapter;
        private ArrayList<H5HotCate> h5HotCates = new ArrayList<>();

        public HeadHolder(LayoutInflater inflater, int resLayoutId) {
            super(inflater, resLayoutId);
            init();
            loadData();
        }

        private void init() {
            //自动轮播
            flashView = (FlashView) view.findViewById(R.id.game_flash_view);
            // 热门推荐
            gridView = (GridView) view.findViewById(R.id.game_gv);
            adapter = new ImageAndTextAdapter(h5HotCates, getActivity());
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(this);
            flashView.setOnPageClickListener(this);
        }

        private void loadData() {
            /**
             * 请求广告栏
             */
            TypeReference<Entity<H5HotCate>> reference = new TypeReference<Entity<H5HotCate>>() {
            };
            AsyncHttpUtils.getInstance().post(UrlContants.URL_GAME_HOT, reference, getHotCateParams(), h5HotCates, adapter);
            /**
             * 请求热门推荐
             */
            AsyncHttpUtils.getInstance().postBannerData(getBannerParams(), flashView, banners);


        }


        private RequestParams getHotCateParams() {
            RequestParams params = new RequestParams();
            params.put("platform", "2");
            params.put("compare", "2306d28d54c3261027a04d9049435295");
            return params;
        }

        private RequestParams getBannerParams() {
            RequestParams params = new RequestParams();
            params.put("platform", "2");
            params.put("compare", "2306d28d54c3261027a04d9049435295");
            params.put("pos", "3");
            return params;
        }


        @Override
        public void onClick(int position) {


        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    }

}
